<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceGame extends Base
{
    protected $gameRankTable = 'finance_game_rank';
    protected $gameRankRewardLogTable = 'game_rank_reward_log';

    //娱乐等级数据缓存
    protected $financeRankLevelGameKey  = 'FINANCE:RANKLEVEL:GAME';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //添加娱乐等级用户奖励领取记录
    public function createGameRankRewardLog($data)
    {
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->gameRankRewardLogTable)->insertGetId($data);
    }

    //更新娱乐等级用户奖励领取记录
    public function updateGameRankRewardLog($id, $data=[])
    {
        $upData = [];
        if(isset($data['if_active_item'])) {
            $upData['if_active_item'] = $data['if_active_item'];
        }
        if(isset($data['if_passive_item'])) {
            $upData['if_passive_item'] = $data['if_passive_item'];
        }
        if(!empty($upData)) {
            DB::table($this->gameRankRewardLogTable)
                ->where('id', $id)
                ->update($upData);
        }

        return 1;
    }

    //财富值明细
    public function getWealthRankData()
    {
        $list = Redis::get($this->financeRankLevelGameKey);
        if(empty($list)){
            $list = DB::table($this->gameRankTable)->get()->toArray();
            $list = self::objToArray($list);
            if(!empty($list)) {
                Redis::setex($this->financeRankLevelGameKey, 86400, json_encode($list));
            }
        }else{
            $list = json_decode($list, true);
        }
        foreach($list as $key=>$val) {
            $val['image'] = build_url($val['image']);
            $val['middle_image'] = build_url($val['middle_image']);
            $val['small_image'] = build_url($val['small_image']);
            $val['join_animation_svg'] = build_url($val['join_animation_svg']);
            $val['big_small_image'] = build_url($val['big_small_image']);
            $val['active_item'] = json_decode($val['active_item'], true);
            $val['passive_item'] = json_decode($val['passive_item'], true);
            $list[$key] = $val;
        }

        return $list;
    }

    //获取娱乐等级用户奖励领取记录
    public function getGameRankRewardListByUserid($userId)
    {
        $list = DB::table($this->gameRankRewardLogTable)->where('user_id', $userId)->get()->toArray();
        $list = self::objToArray($list);
        $list = array_column($list, null, 'level');

        return $list;
    }

    //获取娱乐等级用户奖励未领取升级下发奖励的记录
    public function getGameRankRewardListByNoPassive($userId)
    {
        $list = DB::table($this->gameRankRewardLogTable)->where('user_id', $userId)->where('if_passive_item', 0)->get()->toArray();
        $list = self::objToArray($list);

        return $list;
    }







    //记得删掉
    public function updaterank($where=[], $upData=[])
    {
        DB::table($this->gameRankTable)
            ->where($where)
            ->update($upData);

        return 1;
    }
    //初始化等级奖励 记得删掉
    public function _setRanItemk()
    {
        //data_type 奖励类型 1=礼物 2=商品
        //data_num 礼物数量/商品时间

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 67,
                'data_num' => 1,
                'desc' => '背包礼物（30钻石）-巧克力圣代',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 71,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（大侠）',
            ],
        ];
        $res = $this->updaterank(['level'=>6], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 66,
                'data_num' => 1,
                'desc' => '背包礼物（90钻石）-啵啵奶茶',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 95,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（掌门）',
            ],
            [
                'data_type' => 2,
                'data_id' => 102,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（掌门）',
            ],
        ];
        $res = $this->updaterank(['level'=>11], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 70,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（掌门）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>13], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 124,
                'data_num' => 1,
                'desc' => '背包礼物（360钻石）-浪漫惊喜',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 96,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（宗师）',
            ],
            [
                'data_type' => 2,
                'data_id' => 103,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（宗师）',
            ],
        ];
        $res = $this->updaterank(['level'=>16], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 69,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（宗师）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>18], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 125,
                'data_num' => 1,
                'desc' => '背包礼物（1400钻石）-香槟',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 97,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（盟主）',
            ],
            [
                'data_type' => 2,
                'data_id' => 104,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（盟主）',
            ],
        ];
        $res = $this->updaterank(['level'=>21], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 68,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（盟主）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>23], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 57,
                'data_num' => 86400*30,
                'desc' => '尊贵座驾（盟主）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>25], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 126,
                'data_num' => 1,
                'desc' => '背包礼物（5600钻石）-天外飞仙',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 98,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（国王）',
            ],
            [
                'data_type' => 2,
                'data_id' => 105,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（国王）',
            ],
        ];
        $res = $this->updaterank(['level'=>26], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 67,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（国王）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>28], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 58,
                'data_num' => 86400*30,
                'desc' => '尊贵座驾（国王）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>30], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 127,
                'data_num' => 1,
                'desc' => '背包礼物（22500钻石）-时空穿梭',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 99,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（大帝）',
            ],
            [
                'data_type' => 2,
                'data_id' => 106,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（大帝）',
            ],
            [
                'data_type' => 2,
                'data_id' => 84,
                'data_num' => 100*365*86400,
                'desc' => '精美游戏皮肤（大帝）',
            ],
        ];
        $res = $this->updaterank(['level'=>31], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 66,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（大帝）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>33], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 59,
                'data_num' => 86400*30,
                'desc' => '尊贵座驾（大帝）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>35], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 128,
                'data_num' => 1,
                'desc' => '背包礼物（60000钻石）-浪漫四季',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 100,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（天神）',
            ],
            [
                'data_type' => 2,
                'data_id' => 107,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（天神）',
            ],
            [
                'data_type' => 2,
                'data_id' => 93,
                'data_num' => 100*365*86400,
                'desc' => '精美游戏皮肤（天神）',
            ],
        ];
        $res = $this->updaterank(['level'=>36], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 65,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（天神）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>38], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 60,
                'data_num' => 86400*30,
                'desc' => '尊贵座驾（天神）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>40], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 129,
                'data_num' => 1,
                'desc' => '背包礼物（210000钻石）-龙王之权',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
            [
                'data_type' => 2,
                'data_id' => 101,
                'data_num' => 100*365*86400,
                'desc' => '娱乐头像框（天尊）',
            ],
            [
                'data_type' => 2,
                'data_id' => 108,
                'data_num' => 100*365*86400,
                'desc' => '等级专属气泡（天尊）',
            ],
            [
                'data_type' => 2,
                'data_id' => 94,
                'data_num' => 100*365*86400,
                'desc' => '精美游戏皮肤（天尊）',
            ],
        ];
        $res = $this->updaterank(['level'=>41], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 64,
                'data_num' => 86400*30,
                'desc' => '尊贵头像框（天尊）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>42], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 2,
                'data_id' => 61,
                'data_num' => 86400*30,
                'desc' => '尊贵座驾（天尊）*30天',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>44], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

        //主动领取奖励
        $active_item = [
            [
                'data_type' => 1,
                'data_id' => 130,
                'data_num' => 1,
                'desc' => '背包礼物（420000钻石）-良宵今宵',
            ],
        ];
        //解锁下发的奖励
        $passive_item = [
        ];
        $res = $this->updaterank(['level'=>45], ['active_item'=>json_encode($active_item), 'passive_item'=>json_encode($passive_item)]);

    }

}
